package com.link2room.ironhide.facade.impl;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.PinyinUtil;
import com.link2room.common.util.StringUtil;
import com.link2room.ironhide.dto.param.manager.HotelsManagerParamDto;
import com.link2room.ironhide.dto.param.manager.L2RGroupAreaManagerParamDto;
import com.link2room.ironhide.dto.result.base.PositionBaseDto;
import com.link2room.ironhide.entity.*;
import com.link2room.ironhide.facade.IL2RPositionsFacadeService;
import com.link2room.ironhide.service.IL2RPositionService;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sss on 2017/6/28.
 */
@Component("l2RPositionsFacadeServiceImpl")
public class L2RPositionsFacadeServiceImpl implements IL2RPositionsFacadeService {

    @Resource
    private IL2RPositionService l2RPositionService;

    @Resource
    private ICommonService commonService;

    /**
     * 基础城市属性信息列表
     * @throws Exception
     */
    @Override
    public List<PositionBaseDto> listPositionBase(L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto)throws Exception{
        L2RDistrict queryD=new L2RDistrict();
        queryD.setCityCode(l2RGroupAreaManagerParamDto.getCityCode());
        QueryResult<L2RDistrict> l2RDistrictResult = commonService.findAllEQ(L2RDistrict.class, queryD);
        List<L2RDistrict> districtList = l2RDistrictResult.getResultlist();
        PositionBaseDto topL2RPositionBaseDto=new PositionBaseDto();
        topL2RPositionBaseDto.setLhotelGroupCode(l2RGroupAreaManagerParamDto.getLhotelGroupCode());
        topL2RPositionBaseDto.setCityCode(l2RGroupAreaManagerParamDto.getCityCode());
        topL2RPositionBaseDto.setPositionType("ROOT");
        topL2RPositionBaseDto.setPositionCode("District");
        topL2RPositionBaseDto.setSearchCode("xingzhengqu");
        topL2RPositionBaseDto.setName("行政区");
        List<PositionBaseDto> childL2RPositionBaseList=new ArrayList<>();
        if(districtList!=null&&districtList.size()>0){
            for(L2RDistrict district:districtList){
                if(!"市辖区".equals(district.getDistrictName())) {
                    PositionBaseDto childL2RPositionBaseDto=new PositionBaseDto();
                    childL2RPositionBaseDto.setLhotelGroupCode(l2RGroupAreaManagerParamDto.getLhotelGroupCode());
                    childL2RPositionBaseDto.setCityCode(l2RGroupAreaManagerParamDto.getCityCode());
                    childL2RPositionBaseDto.setPositionType("District");
                    childL2RPositionBaseDto.setPositionCode(district.getDistrictCode().toString());
                    childL2RPositionBaseDto.setSearchCode(district.getSearchCode());
                    childL2RPositionBaseDto.setName(district.getDistrictName());
                    childL2RPositionBaseDto.setChildren(new ArrayList<>());
                    childL2RPositionBaseDto.setSearchCode(district.getSearchCode());
                    childL2RPositionBaseList.add(childL2RPositionBaseDto);
                }
            }
        }
        topL2RPositionBaseDto.setChildren(childL2RPositionBaseList);
        List<PositionBaseDto> topL2RPositionBaseList=new ArrayList<>();
        topL2RPositionBaseList.add(topL2RPositionBaseDto);
        L2RPositionBase queryRb=new L2RPositionBase();
        queryRb.setCityCode(l2RGroupAreaManagerParamDto.getCityCode());
        topL2RPositionBaseList.addAll(l2RPositionService.listPositionBase(l2RGroupAreaManagerParamDto.getLhotelGroupCode(),null,queryRb,L2RPositionBase.class));
        return topL2RPositionBaseList;
    }

    /**
     * 集团在用基础城市属性信息列表
     * @param l2RGroupAreaManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public List<PositionBaseDto> listPositionBaseGroupUse(L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto)throws Exception{
        List<PositionBaseDto> l2rPositionList =this.listPositionBase(l2RGroupAreaManagerParamDto);
        L2RGroupPositionBase queryGroupRb=new L2RGroupPositionBase();
        queryGroupRb.setLhotelGroupCode(l2RGroupAreaManagerParamDto.getLhotelGroupCode());
        queryGroupRb.setCityCode(l2RGroupAreaManagerParamDto.getCityCode());
        List<PositionBaseDto> l2rGroupPositionList = l2RPositionService.listPositionBase(l2RGroupAreaManagerParamDto.getLhotelGroupCode(), null, queryGroupRb, L2RGroupPositionBase.class);
        return this.getUseL2rPositionDto(l2rPositionList,l2rGroupPositionList);
    }

    /**
     * 酒店在用基础城市属性信息列表
     * @param l2RGroupAreaManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public List<PositionBaseDto> listPositionBaseHotelUse(L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto)throws Exception{
        L2RGroupPositionBase queryGroupRb=new L2RGroupPositionBase();
        queryGroupRb.setLhotelGroupCode(l2RGroupAreaManagerParamDto.getLhotelGroupCode());
        queryGroupRb.setCityCode(l2RGroupAreaManagerParamDto.getCityCode());
        List<PositionBaseDto> l2rGroupPositionList = l2RPositionService.listPositionBase(l2RGroupAreaManagerParamDto.getLhotelGroupCode(), null, queryGroupRb, L2RGroupPositionBase.class);

        L2RHotelPosition queryHotelRb=new L2RHotelPosition();
        queryHotelRb.setLhotelGroupCode(l2RGroupAreaManagerParamDto.getLhotelGroupCode());
        if(StringUtil.isNotBlank(l2RGroupAreaManagerParamDto.getLhotelCode())){
            queryHotelRb.setLhotelCode(l2RGroupAreaManagerParamDto.getLhotelCode());
        }else {
            queryHotelRb.setLhotelCode(DefaultValue.LHOTEL_CODE);
        }
        List<PositionBaseDto> l2rHotelPositionList = l2RPositionService.listPositionBase(l2RGroupAreaManagerParamDto.getLhotelGroupCode(), null, queryHotelRb, L2RHotelPosition.class);

        return this.getUseL2rPositionDto(l2rGroupPositionList,l2rHotelPositionList);
    }

    /**
     * 保存城市属性
     * @param l2RGroupAreaManagerParamDto
     * @throws Exception
     */
    @Override
    public void saveL2RGroupPositionBase(L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto)throws Exception{
        L2RGroupPositionBase queryLGP=new L2RGroupPositionBase();
        queryLGP.setLhotelGroupCode(l2RGroupAreaManagerParamDto.getLhotelGroupCode());
        queryLGP.setCityCode(l2RGroupAreaManagerParamDto.getCityCode());
        commonService.deleteAllEQ(L2RGroupPositionBase.class,queryLGP);

        List<PositionBaseDto> l2rGroupPositionList = l2RGroupAreaManagerParamDto.getL2RGroupPositionBases();

        List<PositionBaseDto> l2rGroupPositionBaseList=this.getAllL2RPositionDto(l2rGroupPositionList);
        commonService.saveList(ClassConverUtil.copyPropertiesList(l2rGroupPositionBaseList,L2RGroupPositionBase.class));
    }

    /**
     * 保存或更新酒店属性
     * @param hotelsManagerParamDto
     * @throws Exception
     */
    @Override
    public void saveOrUpdateL2RHotelPosition(HotelsManagerParamDto hotelsManagerParamDto)throws Exception{
        if(hotelsManagerParamDto.getL2RHotelPositions()!=null){
            List<PositionBaseDto> positionBaseDtos= this.getAllL2RHotelPositionDto(hotelsManagerParamDto.getLhotelGroupCode(),hotelsManagerParamDto.getLhotelCode(),hotelsManagerParamDto.getL2RHotelPositions());
            if(positionBaseDtos!=null&&positionBaseDtos.size()>0){
                l2RPositionService.saveOrUpdateHotelPositions(hotelsManagerParamDto.getLhotelGroupCode(),
                        hotelsManagerParamDto.getLhotelCode(),
                        ClassConverUtil.copyPropertiesList(positionBaseDtos,L2RHotelPosition.class));
            }
        }
    }

    private List<PositionBaseDto> getAllL2RHotelPositionDto(String lhotelGroupCode,String lhotelCode,List<PositionBaseDto> l2rHotelPositionList) throws Exception {
        List<PositionBaseDto> l2rHotelPositionBaseList=new ArrayList<>();
        for(PositionBaseDto l2rHotelPosition:l2rHotelPositionList){
            l2rHotelPosition.setIsHalt(DefaultValue.T);
            l2rHotelPosition.setLhotelCode(lhotelCode);
            if(DefaultValue.T.equals(l2rHotelPosition.getIsUse())) {
                l2rHotelPosition.setIsHalt(DefaultValue.F);
                //反向更新酒店基础信息表中区域字段
                if(StringUtil.isNotBlank(lhotelCode)){
                    if("District".equals(l2rHotelPosition.getPositionType())){
                        L2RHotelBaseinfo queryhb=new L2RHotelBaseinfo();
                        queryhb.setLhotelGroupCode(lhotelGroupCode);
                        queryhb.setLhotelCode(lhotelCode);
                        L2RHotelBaseinfo l2RHotelBaseinfo=commonService.findOneEQ(L2RHotelBaseinfo.class,queryhb);
                        if(l2RHotelBaseinfo!=null){
                            l2RHotelBaseinfo.setDistrict(Long.parseLong(l2rHotelPosition.getPositionCode()));
                            commonService.merge(l2RHotelBaseinfo);
                        }
                    }
                }
            }
            l2rHotelPositionBaseList.add(l2rHotelPosition);
            if(l2rHotelPosition.getChildren()!=null&&l2rHotelPosition.getChildren().size()>0){
                l2rHotelPositionBaseList.addAll(this.getAllL2RHotelPositionDto(lhotelGroupCode,lhotelCode,l2rHotelPosition.getChildren()));
            }
        }
        return l2rHotelPositionBaseList;
    }

    private List<PositionBaseDto> getAllL2RPositionDto(List<PositionBaseDto> l2rPositionList) throws BadHanyuPinyinOutputFormatCombination {
        List<PositionBaseDto> l2rPositionBaseList=new ArrayList<>();
        if(l2rPositionList!=null){
            for(PositionBaseDto l2rGroupPosition:l2rPositionList){
                l2rGroupPosition.setIsHalt(DefaultValue.T);
                if(DefaultValue.T.equals(l2rGroupPosition.getIsUse())) {
                    l2rGroupPosition.setIsHalt(DefaultValue.F);
                }
                l2rGroupPosition.setSearchCode(PinyinUtil.getStringPinYin(l2rGroupPosition.getName()));
                l2rPositionBaseList.add(l2rGroupPosition);
                if(l2rGroupPosition.getChildren()!=null&&l2rGroupPosition.getChildren().size()>0){
                    l2rPositionBaseList.addAll(this.getAllL2RPositionDto(l2rGroupPosition.getChildren()));
                }
            }
        }
        return l2rPositionBaseList;
    }

    /**
     *
     * @param <S> 源CLASS
     * @param <T> 目标CLASS
     * @param l2rPositionList1
     * @param l2rPositionList2
     * @return
     * @throws Exception
     */
    private <S extends PositionBaseDto,T extends PositionBaseDto> List<S> getUseL2rPositionDto(List<S> l2rPositionList1,
                                                                                               List<T> l2rPositionList2)throws Exception{
        for(S p1:l2rPositionList1){
            for(T p2:l2rPositionList2){
                if(p1.getPositionType().equals(p2.getPositionType())&&p1.getPositionCode().equals(p2.getPositionCode())){
                    if(DefaultValue.F.equals(p2.getIsHalt())){
                        p1.setIsUse(DefaultValue.T);
                    }
                    if(p1.getChildren()!=null&&p1.getChildren().size()>0&&
                            p2.getChildren()!=null&&p2.getChildren().size()>0){
                        this.getUseL2rPositionDto(p1.getChildren(),p2.getChildren());
                    }
                }
            }
        }
        return l2rPositionList1;
    }
}
